package com.masterclient.app.modules.timelimited.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.OperateResultBean;
import com.masterclient.app.modules.timelimited.model.TimeProductsBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class TimeLimitedProductsContractPresenter extends TimeLimitedProductsContract.Presenter {
    @Override
    public void getProducts(String location,String  status,String page,String sh_event_map_id) {


        String url = NetEnvManager.BASE_SURL+"2/time_preference/" + "goodsList.html";

        Observable<TimeProductsBean> observable = RetrofitUtil.getInstance().getTimeProducts(url,sh_event_map_id,page);

        Disposable disposable = toSubscribe(observable, new Consumer<TimeProductsBean>() {
            @Override
            public void accept(@NonNull TimeProductsBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showData(bean.getData());
                }else {
                    mView.loadFail(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.loadFail("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void setSaleRemain(String event_map_id, String product_id, String type) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().setSaleRemain(event_map_id,product_id,type), (Consumer<OperateResultBean>) bean -> {
            if ("0".equals(bean.getCode())){
                mView.setSaleRemainSuccess();
            }else {
                mView.setSaleRemainFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.setSaleRemainFail("网络异常，请重试");
        }));
    }
}
